package com.thti.mecool.client.activity;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

import com.thti.mecool.client.adapter.CommunicationShowAdapter;
import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.command.send.communication.CommunicationSend;
import com.thti.mecool.client.model.po.CommunicationItem;
import com.thti.mecool.client.protocol.MessageInfo;
import com.thti.mecool.client.protocol.communication.CommunicationResult;
import com.thti.mecool.client.widget.ProgressDialogWidget;
import com.thti.mecool.client.widget.Toolbar;

public class CommunicationActivity extends BaseActivity{
	private ListView listView;
	List<CommunicationItem> list;
	public static Handler handler;
	private ProgressDialogWidget progressDialogWidget;
	CommunicationResult communicationResult;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.noTitle();
		setContentView(R.layout.mecool_communication);
		Cache.add(this);
		listView=(ListView)findViewById(R.id.mecool_communication_listview);
		initToolbar();
		progressDialogWidget=new ProgressDialogWidget(this);
		progressDialogWidget.setDismissTime(15);
		handler=new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				Bundle bundle=msg.getData();
				if(msg.what==0xCD){
					communicationResult=(CommunicationResult) bundle.getSerializable("comresult");
					progressDialogWidget.dismiss();
					CommunicationShowAdapter adapter=new CommunicationShowAdapter(CommunicationActivity.this, communicationResult.getCommunications());
					listView.setAdapter(adapter);
				}else if(msg.what==0xDD){
					MessageInfo messageinfo=(MessageInfo)bundle.getSerializable("msginfo");
					progressDialogWidget.dismiss();
					isRequestServerSuccess(messageinfo);
				}
			}
			
		};
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		requestToServer();
	}
	
	public void initToolbar(){
		Toolbar toolbar=new Toolbar(this);
		toolbar.setToolbarCentreText("沟通管理");
		Toolbar.left_btn.setOnClickListener(leftbuttonListener);
		Toolbar.right_btn.setOnClickListener(rightbuttonListener);
		Toolbar.right_btn.setImageResource(R.drawable.mecool_add_communication_mark);
		Toolbar.left_btn.setImageResource(R.drawable.mecool_go_back);
	}
	private OnClickListener leftbuttonListener=new ImageButton.OnClickListener(){

		@Override
		public void onClick(View v) {
			finish();
		}};
		
	private OnClickListener rightbuttonListener=new ImageButton.OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent=new Intent(CommunicationActivity.this,AddCommicationRecordActivity.class);
			startActivity(intent);
		}
		
	};
	
	private void requestToServer(){
		CommunicationSend.searchCommunication(BaseActivity.getCurrentUser().getLoginName(), 0, 10);
	}
	
	public void isRequestServerSuccess(MessageInfo messageInfo){
		if(messageInfo.getCode().equals("user003")){
			Toast.makeText(CommunicationActivity.this, "记录不存在", 5000).show();
		}
			
	}

	
	
}
